using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using AuthService.Application.OAuth;
using AuthService.Domain.OAuth;

namespace AuthService.EntityFrameworkCore.OAuth
{
    /// <summary>
    /// OAuth客户端仓储实现（简化版）
    /// </summary>
    public class OAuthClientRepository : EfCoreRepository<AuthServiceDbContext, OAuthClient, Guid>, IOAuthClientRepository
    {
        public OAuthClientRepository(IDbContextProvider<AuthServiceDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        public async Task<OAuthClient> GetByClientIdAsync(string clientId, CancellationToken cancellationToken = default)
        {
            return await DbSet
                .FirstOrDefaultAsync(x => x.ClientId == clientId, GetCancellationToken(cancellationToken));
        }

        public async Task<List<OAuthClient>> GetActiveClientsAsync(Guid? tenantId, CancellationToken cancellationToken = default)
        {
            return await DbSet
                .Where(x => x.IsActive && (!tenantId.HasValue || x.TenantId == tenantId))
                .ToListAsync(GetCancellationToken(cancellationToken));
        }

        public async Task<List<OAuthClient>> GetListAsync(string filter = null, int maxResultCount = 10, int skipCount = 0, CancellationToken cancellationToken = default)
        {
            var query = DbSet.AsQueryable();

            if (!string.IsNullOrEmpty(filter))
            {
                query = query.Where(x =>
                    x.ClientName.Contains(filter) ||
                    x.ClientId.Contains(filter));
            }

            return await query
                .Skip(skipCount)
                .Take(maxResultCount)
                .ToListAsync(GetCancellationToken(cancellationToken));
        }

        public async Task<long> GetCountAsync(string filter = null, CancellationToken cancellationToken = default)
        {
            var query = DbSet.AsQueryable();

            if (!string.IsNullOrEmpty(filter))
            {
                query = query.Where(x =>
                    x.ClientName.Contains(filter) ||
                    x.ClientId.Contains(filter));
            }

            return await query.CountAsync(cancellationToken);
        }

        // 简化版实现 - 其他方法根据需要添加
        public async Task<List<OAuthClient>> GetEnabledClientsAsync(CancellationToken cancellationToken = default)
        {
            return await DbSet
                .Where(x => x.IsActive)
                .ToListAsync(GetCancellationToken(cancellationToken));
        }

        public async Task<List<OAuthClient>> GetClientsByGrantTypeAsync(string grantType, CancellationToken cancellationToken = default)
        {
            return await DbSet
                .Where(x => x.GrantTypes.Contains(grantType))
                .ToListAsync(GetCancellationToken(cancellationToken));
        }

        public async Task<List<OAuthClient>> GetClientsByScopeAsync(string scope, CancellationToken cancellationToken = default)
        {
            return await DbSet
                .Where(x => x.Scopes.Contains(scope))
                .ToListAsync(GetCancellationToken(cancellationToken));
        }

        public async Task<bool> ValidateClientSecretAsync(string clientId, string clientSecret, CancellationToken cancellationToken = default)
        {
            var client = await GetByClientIdAsync(clientId, cancellationToken);
            return client != null && client.ClientSecret == clientSecret;
        }

        public async Task<bool> ExistsByClientIdAsync(string clientId, CancellationToken cancellationToken = default)
        {
            return await DbSet
                .AnyAsync(x => x.ClientId == clientId, GetCancellationToken(cancellationToken));
        }
    }
}